<?php
namespace App\Services;
use App\Models\QuestionsModel;
use App\Models\OptionsModel;
use App\Models\TagModel;

class ItemBankService{
	/**
	 * 获取项目选项总条数
	 * @param int $id
	 * @return int $anum
	 */
	public function getOptionsNum(){
		$anum = OptionsModel::where('isshow',1)->where('q_id',$id)->count();
		return $anum;
	}

 	/**
 	 * 获取问题总条数
 	 * @return int $qnum
 	 */
 	public function getQuestionNum(){
 		$qnum = QuestionsModel::where('isshow',1)->count();
 		return $qnum;
 	}

 	/**
 	 * 获取测试题库项目列表
 	 * @param int $pagesize;
 	 * @param string $keywords;
 	 * @return object $question_list
 	 */
 	public function getQuestionList($pagesize,$keywords='',$ordersort='desc'){
 		$question_list = QuestionsModel::orderBy('id',$ordersort)
 						->where('isshow',1)
 						->where(function($query) use ($keywords){
 							if($keywords != ''){
 								$query->where('question', 'like', '%'.$keywords.'%');
 							}
 						})
 						->select('id','question','add_date','tag_id','multi')
 						->paginate($pagesize);
 		foreach($question_list as $qtm=>$qst){
 			$optionlist = $this->getOptionsList($qst->id);
 			$question_list[$qtm]->optionlist = $optionlist;

 			$taginfo = TagModel::where('id',$qst->tag_id)->select('id','tag_name')->first();
 			$question_list[$qtm]->tag_name = $taginfo->tag_name;

 			unset($optionlist);
 		}
 		return $question_list;
 	}

 	/**
 	 * 获取问题详细信息
 	 * @param string $question
 	 * @return object $info
 	 */
 	public function getQuestionInfoByName($question){
 		$info = QuestionsModel::where('question',$question)->where('isshow',1)->select('id')->first();
 		return $info;
 	}

 	/**
 	 * 获取问题详细信息
 	 * @param int $id
	 * @return object $info
 	 */
 	public function getQuestionInfoById($id){
 		$info = QuestionsModel::where('id',$id)->first();
 		// $info->options = OptionsModel::where()->select('id','option')->get();
 		return $info;
 	}

 	/**
 	 * 插入题库问题
 	 * @param array $data
 	 * @return bool $res
 	 */
 	public function insertQuestion($data){
 		$res = QuestionsModel::insert($data);
 		return $res;
 	}

 	/**
 	 * 更新问题
 	 * @param array $data
 	 * @param int $id
 	 * @return bool $res
 	 */
 	public function modifyQuestion($id,$data){
 		$res = QuestionsModel::where('id',$id)->update($data);
 		return $res;
 	}

 	/**
 	 * 添加题库选项
	 * @param array $data
	 * @return bool $res
 	 */
 	public function insertOption($data){
 		$res = OptionsModel::insert($data);
 		return $res;
 	}

 	/**
 	 * 更新问题选项
 	 * @param int $id
 	 * @param array $data
 	 * @return bool $res
 	 */
 	public function modifyOption($id,$data){
 		$res = OptionsModel::where('id',$id)->update($data);
 		return $res;
 	}

 	/**
 	 * 获取问题选项
 	 * @param string $options
 	 * @return bool $res
 	 */
 	public function getOptionByName($options){
 		$res = OptionsModel::where('option',$options)->where('isshow',1)->first();
 		return $res;
 	}

 	/**
 	 * 获取问题选项
 	 * @param int $id
 	 * @return bool $res
 	 */
 	public function getOptionById($id){
 		$res = OptionsModel::where('id',$id)->first();
 		return $res;
 	}

 	/**
 	 * 获取项目选项列表
 	 * @param int $id
 	 * @param object $optionlist
 	 */
 	public function getOptionsList($id){
 		$optionlist = OptionsModel::where('q_id',$id)->where('isshow',1)->select('id','option','add_date')->get();
 		return $optionlist;
 	}
 }